package com.ffcs.crmd.tsp.task.core;

import com.ffcs.crmd.tsp.api.dto.heart.HeartbeatData;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.netty.INettyConfigClient;
import com.ffcs.crmd.tsp.api.netty.INettyRemotingClient;
import com.ffcs.crmd.tsp.common.exception.TspRemotingException;
import com.ffcs.crmd.tsp.common.netty.RequestCode;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.task.netty.NettyRemotingClient;
import com.ffcs.crmd.tsp.task.netty.processor.InterrruptJobDetailFinishProcessor;
import com.ffcs.crmd.tsp.task.netty.processor.InterrruptJobDetailProcessor;
import com.ffcs.crmd.tsp.task.netty.processor.JobDetailProcessor;
import com.ffcs.crmd.tsp.task.netty.processor.JobDetailStateProcessor;
import com.ffcs.crmd.tsp.task.netty.processor.QueryJobDetailThreadInfoProcessor;
import com.ffcs.crmd.tsp.task.netty.processor.UpdateThreadPoolProcessor;

/**
 * 
 * 功能说明:客户端远程通信处理服务实现
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class TspTaskImpl {
    
    /**
     * 执行端通信层
     */
    private final INettyRemotingClient            nettyRemotingClient;
    
    private final JobDetailStateProcessor<Object> jobDetailStateProcessor;
    
    private final JobDetailProcessor<Object>      jobDetailProcessor;
    
    public TspTaskImpl(INettyConfigClient nettyConfig) {
        nettyRemotingClient = new NettyRemotingClient(nettyConfig);
        jobDetailStateProcessor = new JobDetailStateProcessor<Object>(nettyRemotingClient);
        jobDetailProcessor = new JobDetailProcessor<Object>(nettyRemotingClient);
    }
    
    /**
     * 
     * 功能说明:启动
     *
     */
    public void start() {
        nettyRemotingClient.registerProcessor(RequestCode.RUN_JOB_DETAIL, jobDetailProcessor)
            .registerProcessor(RequestCode.RUN_JOB_DETAIL_STATFUL, jobDetailStateProcessor)
            .registerProcessor(RequestCode.UPDATE_THREAD_POOL, new UpdateThreadPoolProcessor<Object>(nettyRemotingClient))
            .registerProcessor(RequestCode.INTERRUPT_JOB_DETAIL_RUNNING, new InterrruptJobDetailProcessor(nettyRemotingClient))
            .registerProcessor(RequestCode.INTERRUPT_JOB_DETAIL_RUNNING_FINISH, new InterrruptJobDetailFinishProcessor(nettyRemotingClient))
            .registerProcessor(RequestCode.QUERY_JOB_DETAIL_THREAD_INFO, new QueryJobDetailThreadInfoProcessor(nettyRemotingClient));
        nettyRemotingClient.start();
    }
    
    /**
     * 
     * 功能说明:关闭
     *
     */
    public void shutdown() {
        nettyRemotingClient.shutdown();
    }
    
    /**
     * 
     * 功能说明:发送心跳
     * 
     * @param serverAddr 服务端地址
     * @param heartbeatData 心跳数据
     * @return 请求结果
     * @throws TspRemotingException 
     */
    public void sendHeartBeat(String serverAddr, HeartbeatData heartbeatData) throws TspRemotingException {
        String request = XmlUtil.toXml(heartbeatData);
        RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.HEART_BEAT);
        remotingCommand.setBody(request.getBytes());
        nettyRemotingClient.invokeOneway(serverAddr, remotingCommand, null);
    }
    
    /**
     * 
     * 功能说明:
     *
     */
    public void retryReturnCompleteJobDetail(){
        jobDetailProcessor.retryReturnCompleteJobDetail();
        jobDetailStateProcessor.retryReturnCompleteJobDetail();
    }
    
    
}
